package node;

import javax.media.j3d.*;
import javax.vecmath.Tuple3d;

import component.AbstractComponent;

import math.Spherical;

abstract public class RotatableGeoNode extends GeoNode {
	
	protected final Spherical orientation = new Spherical();
	
	private Transform3D currentTransform = new Transform3D();
	
	private TransformGroup rotationGroup = new TransformGroup();
	
	@Deprecated
	public RotatableGeoNode(Tuple3d position, int status) {
		this(null, position, 0, 0, status);
	}
	
	@Deprecated
	public RotatableGeoNode(AbstractComponent parent, Tuple3d position, double zRotation, double yRotation, int status) {
		super(parent, position, status);
		this.setRotation(zRotation, yRotation);
		rotationGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		rotationGroup.setCapability(TransformGroup.ALLOW_LOCAL_TO_VWORLD_READ);
		
		this.getTranslationGroup().addChild(rotationGroup);
	}

	public RotatableGeoNode(AbstractComponent parent, Tuple3d position, Spherical orientation, int status) {
		super(parent, position, status);
		this.setOrientation(orientation);
		rotationGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		rotationGroup.setCapability(TransformGroup.ALLOW_LOCAL_TO_VWORLD_READ);
		
		this.getTranslationGroup().addChild(rotationGroup);
	}
	
	public double getZOrientation() {
		return this.orientation.getZ();
	}
	
	public double getYOrientation() {
		return this.orientation.getY();
	}
	
	public void setZOrientation(double zRotation) {
		this.orientation.setZ(zRotation);
		this.updateRotation();
	}
	
	public void setYOrientation(double yRotation) {
		this.orientation.setY(yRotation);
		this.updateRotation();
	}
	
	@Deprecated
	public void setRotation(double zRotation, double yRotation) {
		this.orientation.setZ(zRotation);
		this.orientation.setY(yRotation);
		this.updateRotation();
	}
	
	public void setOrientation(Spherical orientation) {
		this.orientation.set(orientation);
		this.updateRotation();
	}
	
	public Spherical getOrientation() {
		return new Spherical(orientation);
	}
	
	protected void updateRotation() {
//		this.checkRotationBounds();
		Transform3D zTrans = new Transform3D();
		zTrans.rotZ(orientation.getZ());
		Transform3D yTrans = new Transform3D();
		yTrans.rotY(orientation.getY());
		
		currentTransform.mul(yTrans, zTrans);
		rotationGroup.setTransform(currentTransform);
	}
	
	protected TransformGroup getRotationGroup() {
		return this.rotationGroup;
	}


}
